<template>
  <div>
    <el-header>API管理系统</el-header>
    <el-input placeholder="请输入内容" v-model="input" clearable class="input"></el-input>
    <el-button type="primary" @click="doFilter(input)">搜索</el-button>
    <el-button type="primary" @click="addpush">添加</el-button>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <!-- 添加的模态框 -->
      <el-form :model="form">
        <el-form-item label="api名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="接口名称" :label-width="formLabelWidth">
          <el-input v-model="form.nick" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请求类型" :label-width="formLabelWidth">
          <el-input v-model="form.mode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="响应类型" :label-width="formLabelWidth">
          <el-input v-model="form.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-input v-model="form.state" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sooket">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
      stripe
      style="width: 100%"
    >
      <el-table-column prop="name" label="api名称" width="180"></el-table-column>
      <el-table-column prop="nick" label="接口名称" width="180"></el-table-column>
      <el-table-column prop="mode" label="请求类型" width="180"></el-table-column>
      <el-table-column prop="type" label="响应类型" width="180"></el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
// import axios from "axios";
export default {
  data() {
    return {
      tableData: [], //列表数据为空 接收axios获取的数据
      input: "", //input框数据为空
      currentPage: 1, //初始页
      pagesize: 10, //每页数据
      formLabelWidth: "120px", //添加模态框文字宽度
      dialogFormVisible: false, //添加模态框  默认不显示
      //添加模态框数据
      form: {
        name: ""
      }
    };
  },
  created() {
    //   钩子函数 载入发送axios请求获取列表页面
    this.getData();
  },
  methods: {
    //添加数据
    addpush() {
      this.dialogFormVisible = true;
    },
    //添加模态框  点击确定
    sooket() {
      this.tableData.push({
        name: this.form.name,
        nick: this.form.nick,
        mode: this.form.mode,
        type: this.form.type,
        state: this.form.state
      });
      this.dialogFormVisible = false;
      // 重置表单的样式
      this.tableData();
      this.$message.sussess("添加成功");
    },
    // 跳转到demo页面   
    handleEdit(id) {
      this.$router.push({
        path: "/demo",
        query:id
        // query: index   //路由传参
      });
      
      sessionStorage.setItem("testKey", "这是一个测试的value值"); // 存入一个值
      sessionStorage["testKey"] = "这是一个测试的index值";
    },
    // 分页
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },
    //axios请求获取列表数据
    getData(res) {
      this.axios.get("../../static/list.json").then(res => {
        this.tableData = res.data.list;
      // console.log(this.tableData)
      }),
        res => {
          alert("请求失败");
        };
 
    },
    //删除功能
    handleDelete(index) {
      // console.log(role)
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
         
          this.tableData.splice(index, 1);
          //  if(this.tableData.length === 1) {
          //    alert(this.tableData.length)
          //   this.currentPage--
          // }
          
          this.$message({
            type: "success",
            message: "删除成功！"
            
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "删除失败"
          });
        });
       console.log(index)
    },
    //input框搜索
    doFilter(res) {
      if (res === "") {
        this.$message.warning("查询条件不能为空");
        return;
      }
      this.current = 1;
      //关键词搜索
      this.tableData = this.tableData.filter(
        item =>
          item.name.includes(res) || item.nick.includes(res) || item.state.includes(res)
         
         
      );
      console.log(res);
    }
  }
};
</script>

<style>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
.input {
  width: 180px;
  margin: 10px;
}
</style>
